/**
 * 拍照上传
 */
import {UploadBase64Img} from '@/api/api.js'
export default{
  data(){
    return {
      imgList:[],//照片列表
    }
  },
  methods: {
    captureImage(){
      /* eslint-disable */
      return new Promise((resolve,reject)=>{

        var cmr = plus.camera.getCamera();
        var res = cmr.supportedImageResolutions[0];
        var fmt = cmr.supportedImageFormats[0];
        cmr.captureImage( path =>{
             plus.io.resolveLocalFileSystemURL(path, entry=> {
               var image = new Image();
               image.src = entry.toLocalURL()
                console.log(image.width,image.height)
                image.onload = ()=> {
                  var imgData = this.getBase64Image(image);
                  console.log(imgData.length)
                  console.log(imgData)
                  // 先上传，获取图片路径
                  // 上传单张图片，返回图片路径 
                  UploadBase64Img({
                    Base64Photo: imgData,
                    fileext:'.png',
                  }).then(res=>{
                    console.log('上传成功：')
                    console.log(JSON.stringify(res))
                    console.log(JSON.stringify(res.Detiel))
                    // localStorage.setItem('imgData',JSON.stringify(res.Detiel[0]))
                    this.imgList.push(res.Detiel[0])
                    this.show = false;
                    resolve(this.imgList)
                  })
  
                }
            }, function(e) {
              plus.nativeUI.toast("读取拍照文件错误：" + e.message);
              reject(false)
            });
          },
          error => {
            // this.$toast('提示文案err',error.message);
            Message({message:'出错',type:'warning'})
            reject(false)
          },
          {resolution:res,format:fmt}
        );
      })
    },
    /**
     * @name 获取相册图片
     */
    gettingAlbums() {
      let that = this;
      return new Promise((resolve,reject)=>{
        plus.gallery.pick(function(e) {
          var image = new Image();
          image.src = e
          var name = e.substr(e.lastIndexOf('/') + 1);
          var fileext = name.split(".")[1];
          image.onload = ()=> {
            var imgData = that.getBase64Image(image);
            UploadBase64Img({
              Base64Photo: imgData,
              fileext:'.png',
            }).then(res=>{
              that.imgList.push(res.Detiel[0])
              resolve(that.imgList)
            })

          }

          // var name = e.substr(e.lastIndexOf('/') + 1);
          // var imgData = this.compressImage(e,name); 
          // UploadBase64Img({
          //   Base64Photo: imgData,
          //   fileext:'.png',
          // }).then(res=>{
          //   console.log('上传成功：')
          //   console.log(JSON.stringify(res))
          //   console.log(JSON.stringify(res.Detiel))
          //   // localStorage.setItem('imgData',JSON.stringify(res.Detiel[0]))
          //   this.imgList.push(res.Detiel[0])
          //   this.show = false;
          //   resolve(this.imgList)
          // })

        }, function(e) {
          plus.nativeUI.toast("读取相册文件失败：" + e.message);
          reject(false)
        }, {
          filter: "image"
        });
      })
    },
    //将图片压缩转成base64 
    getBase64Image(img) { 
        var canvas = document.createElement("canvas"); 
        var width = img.width; 
        var height = img.height; 
        // calculate the width and height, constraining the proportions 
        if (width > height) { 
            if (width > 750) { 
                height = Math.round(height *= 750 / width); 
                width = 750; 
            } 
        } else { 
            if (height > 750) { 
                width = Math.round(width *= 750 / height); 
                height = 750; 
            } 
        } 
        canvas.width = width;   /*设置新的图片的宽度*/ 
        canvas.height = height; /*设置新的图片的长度*/ 
        var ctx = canvas.getContext("2d"); 
        ctx.drawImage(img, 0, 0, width, height); /*绘图*/ 
        var dataURL = canvas.toDataURL("image/png", 0.8); 
        return dataURL; 
    },
    /**
     * @name 压缩图片
     * @param {String} url 图片路劲
     * @param {String} filename 图片名称
     */
    compressImage(url, filename){
      var name="_doc/upload/" + filename;
      plus.zip.compressImage({
        src:url,//src: (String 类型 )压缩转换原始图片的路径
        dst:name,//压缩转换目标图片的路径
        quality:60,//quality: (Number 类型 )压缩图片的质量.取值范围为1-100
        overwrite:true//overwrite: (Boolean 类型 )覆盖生成新文件
      },
      function(zip) {
        console.log('图片压缩后路径' + zip.target)
        return zip.target
      },function(error) {
        plus.nativeUI.toast("压缩图片失败，请稍候再试");
      });
      }
  }
}